const router = require('koa-router')()

// 模拟歌曲数据库
const songs = [
  {
    id: 1,
    name: '骄傲',
    artist: '王源',
    cover: 'https://ai-public.mastergo.com/ai/img_res/9b30f4ee0302b8d25ce0fca941c22eca.jpg',
    url: '/music/wangyuan/jiaoao.mp3',
    duration: 240
  },
  {
    id: 2,
    name: '夜的钢琴曲',
    artist: '石进',
    cover: 'https://ai-public.mastergo.com/ai/img_res/c1ce38ecf2bd6fbe61281df433ca6b50.jpg',
    url: '/music/shijin/yedegangqinqu.mp3',
    duration: 180
  },
  {
    id: 3,
    name: '童话镇',
    artist: '陈一发儿',
    cover: 'https://ai-public.mastergo.com/ai/img_res/f9fc38e361ab67a35c0e5506eddd71df.jpg',
    url: '/music/chenyifaer/tonghuazhen.mp3',
    duration: 210
  },
  {
    id: 4,
    name: '起风了',
    artist: '买辣椒也用券',
    cover: 'https://ai-public.mastergo.com/ai/img_res/45336b12831b6117c08a852940c79512.jpg',
    url: '/music/malajiaoyeyongquan/qifengle.mp3',
    duration: 195
  }
]

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

// 搜索歌曲API
router.get('/api/search', async (ctx, next) => {
  const { keyword } = ctx.query
  
  if (!keyword || keyword.trim() === '') {
    ctx.body = {
      code: 400,
      message: '请输入搜索关键词',
      data: []
    }
    return
  }
  
  const searchTerm = keyword.toLowerCase().trim()
  const results = songs.filter(song => 
    song.name.toLowerCase().includes(searchTerm) || 
    song.artist.toLowerCase().includes(searchTerm)
  )
  
  ctx.body = {
    code: 200,
    message: '搜索成功',
    data: results
  }
})

// 获取所有歌曲API
router.get('/api/songs', async (ctx, next) => {
  ctx.body = {
    code: 200,
    message: '获取成功',
    data: songs
  }
})

module.exports = router
